package com.qfedu.microuser.user.controller;

import com.qfedu.common.core.annotation.NoLogin;
import com.qfedu.common.core.common.R;
import com.qfedu.common.core.interceptor.TimeInterceptor;
import com.qfedu.microuser.user.param.LoginParam;
import com.qfedu.microuser.user.param.RegisterParam;
import com.qfedu.microuser.user.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * @Classname LoginController
 * @Description TODO
 * @Date 2025-09-22 16:21
 * @Created by 老任与码
 */
// 使验证注解生效
@Validated
//@CrossOrigin("*")
@RestController
@RequestMapping("/api/user")
public class LoginController {

    @Resource
    private UserService userService;

    //@Valid递归验证
    @NoLogin
    @PostMapping("/login")
    public R login(@Valid @RequestBody LoginParam loginParam) {
        String token = userService.login(loginParam);
        return R.success(token);
    }

    @GetMapping("/send/code")
    public R sendCode(@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式错误") String phone) {
        userService.sendCode(phone);
        return R.success();
    }

    @NoLogin
    @PostMapping("/register")
    public R register(@RequestBody RegisterParam registerParam) {
        userService.register(registerParam);
        return R.success();
    }

    @PostMapping("/login2")
    public R login(@NotBlank(message = "请输入用户名") String username, String loginParam) {
        // String token = userService.login(loginParam);
        return R.success();
    }

    @NoLogin
    @GetMapping("/time")
    public R testTime() {
        long begin = TimeInterceptor.threadLocal.get();
        System.out.println("time:" + begin);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("time");
        return R.success();
    }
}
